﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.Utils;
using Jn698._45Tool.Service;
using log4net;
using nblTool.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class CONNECTControlViewModel:ViewModelBase
    {
        public static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private GWCmdMakeUpPlanService gWCmdMakeUpPlanService=new GWCmdMakeUpPlanService();
        private ObjectIDService objectIDService = new ObjectIDService();
        public GWCmdMakeUp gWCmdMakeUp = null;
        public ObjectID objectID =null;
        private string version;

        public string Version
        {
            get { return version; }
            set 
            { 
                Set<string>(ref version, value);
                saveData(); 
            }
        }
        private string password;

        public string Password
        {
            get { return password; }
            set { Set(ref password, value); }
        }


        private string agreement;

        public string Agreement
        {
            get { return agreement; }
            set { agreement = value; RaisePropertyChanged(); saveData(); }
        }

        private string func;

        public string Func
        {
            get { return func; }
            set { func = value; RaisePropertyChanged(); saveData(); }
        }

        private string sendMaxSize;

        public string SendMaxSize
        {
            get { return sendMaxSize; }
            set { sendMaxSize = value; RaisePropertyChanged(); saveData(); }
        }

        private string recvMaxSize;

        public string RecvMaxSize
        {
            get { return recvMaxSize; }
            set { recvMaxSize = value; RaisePropertyChanged(); saveData(); }
        }

        private string recvFromMaxSize;

        public string RecvFromMaxSize
        {
            get { return recvFromMaxSize; }
            set { recvFromMaxSize = value; RaisePropertyChanged(); saveData(); }
        }

        private string maxAPDU;

        public string MaxAPDU
        {
            get { return maxAPDU; }
            set { maxAPDU = value; RaisePropertyChanged(); saveData(); }
        }

        private string connectTimeout;

        public string ConnectTimeout
        {
            get { return connectTimeout; }
            set { Set(ref connectTimeout,value); saveData(); }
        }

        private int requstObj;

        public int RequstObj
        {
            get { return requstObj; }
            set { requstObj = value; RaisePropertyChanged(); saveData(); }
        }

        private ObservableCollection<string> requstObjList;

        public ObservableCollection<string> RequstObjList
        {
            get { return requstObjList; }
            set { Set(ref requstObjList,value); }
        }


        private bool connectVisibility;

        public bool ConnectVisibility
        {
            get { 
                return connectVisibility; 
            }
            set { connectVisibility = value; RaisePropertyChanged(); }
        }

        public static Action<bool> action;
       
        public CONNECTControlViewModel()
        {
            LogHelper.WriteInfo(typeof(CONNECTControlViewModel),"应该连接");
            objectID = objectIDService.Select(2);
            gWCmdMakeUp = gWCmdMakeUpPlanService.Select(objectID)[0];
            var strings = gWCmdMakeUp.DataValue.Split(';');
            Version = strings[0];
            Agreement = strings[1];
            Func = strings[2];
            SendMaxSize = strings[3];
            RecvMaxSize = strings[4];
            RecvFromMaxSize = strings[5];
            MaxAPDU = strings[6];
            ConnectTimeout = strings[7];
            RequstObj = Convert.ToUInt16(strings[8]);
            Messenger.Default.Register<bool>(this, "ConnectVisibility", (b) => { ConnectVisibility = b; });
            action = ((b) => { 
                
                ConnectVisibility = true; 
            });
            RequstObjList = new ObservableCollection<string> {"公共连接","一般密码","对称加密","数字签名" };
            //action = SetB;
            Password = "123456";
        }

        private void saveData()
        {
            if (gWCmdMakeUp == null)
            {
                return;
            }
            string value = "";
            value += Version;
            value += ";";
            value += Agreement;
            value += ";";
            value += Func;
            value += ";";
            value += SendMaxSize;
            value += ";";
            value += RecvMaxSize;
            value += ";";
            value += RecvFromMaxSize;
            value += ";";
            value += MaxAPDU;
            value += ";";
            value += ConnectTimeout;
            value += ";";
            value += RequstObj;
            value += ";";
            gWCmdMakeUp.DataValue = value;
            gWCmdMakeUpPlanService.Update(gWCmdMakeUp);
        }
    }
}
